<template>
	<view class="coupon-container">
		<view class="coupon-section">
			<view class="card-box" v-if="coupon_list.length>0">
				<view class="card" :style="{background:t('color2') || '#B969E7'}" v-for="(item,index) in coupon_list"
					:key="index">
					<view class="card-left">
						<view class="card-left-top" v-if="item.type== 41"
							:style="{ background: `linear-gradient(to right, ${t('color2')}, ${t('color1')})` }">
							新人券
						</view>
						<text class="card-left-money">￥</text>
						<text>{{item.money}}</text>
					</view>
					<view class="card-item">
						<view class="card-right">
							<view class="card-time">
								{{item.name}}
							</view>
							<view class="card-right-text">
								<text class="product-title"
									v-if="item.producttitle != null && item.producttitle !== undefined">
									{{item.producttitle}}
								</text>
							</view>
							<view class="card-time" v-if="item.has_record == 0">
								{{ calculateRemainingTime(item.starttime, item.endtime) }}
							</view>
							<view class="card-time" v-if="item.has_record == 1">
								{{ calculateRemainingTime(item.yxqstarttime, item.yxqendtime) }}
							</view>
						</view>
						<view class="card-btn-box">
							<view class="card-btn card-btn-item" :style="{ background: t('color1')  }">
								<view v-if="item.has_record == 0 " @click="handleClaim(item,index)">领取</view>
								<view v-else :data-url="'/pages/shop/product?id=' + opt.id" @click="goto">去使用</view>
							</view>
							<view class="card-btn-text">
								已发放{{item.has_record_count}}张
							</view>
						</view>
					</view>
				</view>

			</view>
			<!-- 推荐商品 -->
			<view class="recommend-product-box">

				<view class="recommend-product-top">
					<view class="recommend-product-top-show">

					</view>
				</view>
				<view class="recommend-product" @tap.stop="goto" :data-url="'/pages/shop/product?id=' + pro_data.pro_id">
					<view class="product-card">
						<view class="product-info">
							<image class="product-image" :src="pro_data.pic">
							</image>
							<view class="product-detail">
								<view class="product-detail-title">{{pro_data.name}}</view>
								<view class="product-detail-text">
									{{pro_data.sellpoint}}
								</view>
								<view class="flex">
									<view class="product-price">¥{{pro_data.sell_price}}</view>
									<view class="product-sold">已售 {{pro_data.sales || 0}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>



		<!-- 热销排行榜 -->
		<view class="hot-sales">
			<view class="hot-sales-title">
				<image mode="widthFix"
					src="https://hedashepin.oss-cn-guangzhou.aliyuncs.com/upload/3/20250320/487f645844f3af35dd803defcbf07254.jpeg"
					class="crown-icon"></image>
				<!-- <text>七日热销排行榜</text> -->
			</view>
			<!-- <view class="hot-sales-subtitle">根据七日销量数据实时更新</view> -->

			<view class="hot-product-list">
				<view class="hot-product-item" v-for="(product, index) in hotProducts" :key="index" @tap.stop="goto" :data-url="'/pages/shop/product?id=' + product.proid">
					<view class="rank-number">{{ index + 1 }}</view>
					<image class="hot-product-image" :src="product.pic"></image>
					<view class="hot-product-info">
						<view class="hot-product-title">{{ product.name }}</view>
						<view class="hot-product-sales">
							<image src="@/pagesSocial/static/img/album.png" class="heart-icon"></image>
							<text>七日成交{{ product.sales || 0 }}件</text>
						</view>
						<view class="flex">
							<view class="hot-product-price">¥{{ product.sell_price }}</view>
							<view class="hot-product-stock">销量：{{ product.sales || 0 }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var app = getApp()
	export default {
		data() {
			return {
				isClaimed: false,
				opt: {},
				coupon_list: [],
				pro_data: {},
				hotProducts: []
			}
		},
		onLoad(opt) {
			this.opt = app.getopts(opt);
			this.getCoupons()
		},
		methods: {
			
			claimCoupon(item, index) {
			    var that = this;
			    app.post('Coupon/getcoupon', {
			        id: item.id,
			    }, function(res) {
			        if (res.status == 1) {
			            app.success(res.msg)
			            that.coupon_list[index].has_record = 1
			            that.$forceUpdate()
			        } else {
			            app.error(res.msg)
			        }
			    });
			},
			handleClaim(item,index) {
				var that = this;
				
				that.loading = true;
				// #ifdef MP-WEIXIN
				app.get('Wxtmpl/getTmplIds', {
					type: 4
				}, function(res) {
					that.loading = false;
			
					var tmplIds = res.res;
					if (res.res) {
						wx.requestSubscribeMessage({
							tmplIds: tmplIds,
							complete(res, err) {
								that.claimCoupon(item,index)
							}
						});
			
					} else {
						
						app.error(res);
						
					}
				});
				// #endif
			// #ifdef H5
			that.claimCoupon(item,index)
			// #endif
			},
			getCoupons() {
				var that = this;
				app.get('coupon/getinviteprocoupon', {
					pro_id: that.opt.id,
				}, function(res) {
					if (res.code == 200) {
						that.coupon_list = res.data.map(item => {
							return {
								...item,
								isClaimed: false
							};
						});
						that.pro_data = res.pro_data
						that.hotProducts = res.hotlist
					} else {
						app.error(res.msg)
					}
				});

			},
			calculateRemainingTime(starttime, endtime) {
				const now = new Date();
				const startTime = new Date(starttime);
				const endTime = new Date(endtime);

				if (now >= startTime && now <= endTime) {
					const timeDiff = endTime - now;
					const days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
					const hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
					const minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
					return `仅剩${days}天${hours}小时${minutes}分钟`;
				} else if (now < startTime) {
					return "优惠券尚未生效";
				} else {
					return "优惠券已过期";
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.card-box {
		padding: 20rpx;
		background: linear-gradient(to bottom, #FFFBFB, #FFF4F4);
		background-attachment: local;
		border-top-left-radius: 40rpx;
		border-top-right-radius: 40rpx;
		margin: 0 20rpx;
		position: relative;
		z-index: 2;
	}

	.card {
		display: flex;
		align-items: center;
		width: 100%;
		background: #fff;
		border-radius: 8px;
		-webkit-mask-image: radial-gradient(circle at 88px 4px, transparent 4px, red 4.5px), radial-gradient(closest-side circle at 50%, red 99%, transparent 100%);
		// -webkit-mask-size: 100%, 2px 4px;
		-webkit-mask-repeat: repeat, repeat-y;
		-webkit-mask-position: 0 -8rpx, 170rpx;

		position: relative;
		margin-bottom: 20rpx;
	}

	.card-left {
		width: 88px;
		text-align: center;
		font-size: 18px;
		color: #fff;
		flex-shrink: 0;
	}

	.card-left-top {
		position: absolute;
		top: 0;
		left: 0;
		font-size: 24rpx;
		padding: 4rpx 12rpx;
		border-bottom-right-radius: 30rpx;
	}

	.card-right {
		padding: 16px 12px;
		display: flex;
		// flex: 1;
		flex-direction: column;
	}

	.card-info {
		margin: 0;
		font-size: 14px;
		line-height: 20px;
		color: #fff;
	}

	.card-time {
		font-size: 12px;
		line-height: 16px;
		font-weight: normal;
		color: #fff;
		margin-top: 4px;
		min-width: 260rpx;
	}

	.card-right-text {
		display: flex;
		align-items: center;
		color: #fff;
	}

	.product-title {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		max-width: 240rpx;
	}

	.card-right-img {
		width: 30rpx;
		height: 30rpx;
	}

	.card-left-money {
		font-size: 24rpx
	}

	.card-btn-box {}

	.card-btn {
		color: #fff;
		border-radius: 30rpx;
		padding: 10rpx 30rpx;
		font-size: 24rpx;
		text-align: center;
	}

	.card-btn-item {
		width: 140rpx;
	}

	.card-btn-text {
		font-size: 24rpx;
		color: #fff;
		text-align: center;
	}

	.card-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex: 1;
		padding: 0 20rpx 0 0;
	}

	.card-item::before {
		content: '';
		position: absolute;

		top: 0;
		bottom: 0;
		width: 2px;
		border-left: 1px dashed #fff;

	}

	.flex {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.coupon-section {

		background: linear-gradient(to bottom, #FFCFB6, #FF9F11);
		background-attachment: local;
		min-height: 200rpx;
		padding: 20rpx 20rpx 0 20rpx;
		overflow: auto;
		position: relative;

		.coupon-card {
			display: flex;
			background: #fff;
			border-radius: 16rpx;
			margin-bottom: 20rpx;
			padding: 30rpx 20rpx;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
			position: relative;

			&::before {
				content: '';
				position: absolute;
				right: 0;
				top: 50%;
				transform: translateY(-50%);
				width: 1rpx;
				height: 80%;
				background: #f0f0f0;
			}

			.coupon-left {
				width: 25%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				border-right: 1rpx dashed #eee;

				.coupon-price {
					color: #ff6b6b;

					.price-symbol {
						font-size: 28rpx;
					}

					.price-value {
						font-size: 48rpx;
						font-weight: bold;
					}
				}


			}

			.coupon-middle {
				width: 45%;
				padding: 0 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;

				.coupon-title {
					font-size: 28rpx;
					font-weight: bold;
					margin-bottom: 10rpx;
				}

				.coupon-time,
				.coupon-desc {
					font-size: 24rpx;
					color: #999;
					margin-top: 6rpx;
				}
			}

			.coupon-right {
				width: 30%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;

				.get-btn {
					background: #ff6b6b;
					color: #fff;
					font-size: 26rpx;
					padding: 10rpx 20rpx;
					border-radius: 30rpx;
					margin-bottom: 10rpx;
				}

				.issued-count {
					font-size: 22rpx;
					color: #999;
				}
			}
		}
	}

	.recommend-product-box {
		position: relative;
		z-index: 1;
		margin-top: -60rpx;
	}

	.recommend-product-top {
		background: #F22E11;
		border-top-right-radius: 40rpx;
		border-top-left-radius: 40rpx;
		width: 100%;
		height: 60rpx;
		position: relative;
	}

	.recommend-product-top-show {

		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		height: 20rpx;
		box-shadow: 0 8rpx 16px rgba(255, 208, 192, 0.8);
		z-index: 3;
	}

	.recommend-product {
		background: linear-gradient(to right, #F6705F, #FD5254);
		padding: 30rpx 20rpx;
		border-bottom-right-radius: 30rpx;
		border-bottom-left-radius: 30rpx;
		position: relative;


		.product-card {
			background: #fff;
			border-radius: 16rpx;
			padding: 20rpx;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

			.product-info {
				display: flex;

				.product-image {
					width: 160rpx;
					height: 160rpx;
					border-radius: 8rpx;
				}

				.product-detail {
					flex: 1;
					padding-left: 20rpx;

					.product-title {
						font-size: 26rpx;
						line-height: 1.4;
						margin-bottom: 20rpx;
					}

					.product-price {
						color: #ff6b6b;
						font-size: 32rpx;
						font-weight: bold;
					}

					.product-sold {
						font-size: 24rpx;
						color: #999;
						margin-top: 10rpx;
					}
				}
			}
		}
	}




	.hot-sales {

		.hot-sales-title {
			display: flex;
			align-items: center;
			justify-content: center;
			

			.crown-icon {
				width: 100%;
				height: 40rpx;

			}

			text {
				font-size: 32rpx;
				font-weight: bold;
			}
		}

		.hot-sales-subtitle {
			text-align: center;
			font-size: 24rpx;
			color: #999;
			margin-bottom: 20rpx;
		}

		.hot-product-list {
			background: #FF9F11;
			padding: 20rpx;
			.hot-product-item {
				display: flex;
				background: #fff;
				border-radius: 16rpx;
				padding: 20rpx;
				margin-bottom: 20rpx;
				box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
				position: relative;

				.rank-number {
					position: absolute;
					left: 10rpx;
					top: 10rpx;
					width: 40rpx;
					height: 40rpx;
					background: #ff6b6b;
					color: #fff;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 24rpx;
					font-weight: bold;
					z-index: 999;
				}

				.hot-product-image {
					width: 160rpx;
					height: 160rpx;
					border-radius: 8rpx;
				}

				.hot-product-info {
					flex: 1;
					padding-left: 20rpx;

					.hot-product-title {
						font-size: 26rpx;
						line-height: 1.4;
						margin-bottom: 10rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.hot-product-sales {
						display: inline-flex;
						align-items: center;
						font-size: 24rpx;
						color: #999;
						margin-bottom: 10rpx;
						background-color: #FEF1F3;
						border-radius: 30rpx;
						padding: 4rpx 10rpx;

						.heart-icon {
							width: 24rpx;
							height: 24rpx;
							margin-right: 6rpx;
						}
					}

					.hot-product-price {
						color: #ff6b6b;
						font-size: 32rpx;
						font-weight: bold;
					}

					.hot-product-stock {
						font-size: 24rpx;
						color: #999;
						margin-top: 6rpx;
					}
				}
			}
		}
	}

	.product-detail-title {
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
		text-overflow: ellipsis;
		font-weight: 700;
	}

	.product-detail-text {
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
		text-overflow: ellipsis;
		font-size: 24rpx;
	}
</style>